<?

namespace Admin;

use Sentry,
    Category,
    Str,
    Config,
    Lang,
    Input,
    Validator,
    Redirect,
    View,
    Jinput,

    AdminController;


class CategoriesController extends AdminController
{

    public function getIndex()
    {
        $data['appends']['withTrashed'] = Jinput::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Jinput::get('onlyTrashed', false);
        $data['appends']['status'] = Jinput::get('status', 'on');
        $myModel = new \MyModel();
        $arrParams = array();
        $arrParams['paginate']['appends'] = $data['appends'];
        $arrParams['conditions'][] = array('field' => 'status', 'opr' => '=', 'value' => $data['appends']['status']);
        $arrParams['conditions'][] = array('field' => 'parent_id', 'opr' => '=', 'value' => 0);
        $data['categories'] = $myModel->listItems('Category', $arrParams);
        return View::make('backend/categories/index', compact('data'));
    }

    public function getCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['category', 'category.create']))
            return View::make('backend/notallow');
        $data['category'] = new Category();
        $data['levels'] = array();
        return View::make('backend/categories/create', compact('data'));
    }

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['category', 'category.create']))
            return View::make('backend/notallow');
        if (is_null($data['category'] = Category::find($id)))
            return Redirect::route('categories/admin');
        $data['levels'] = Category::where('parent_id', '=', $id)->get();
        return View::make('backend/categories/create', compact('data'));
    }

    public function getDelete($id)
    {

    }

    public function getRestore($id)
    {

    }

    public function postCreate()
    {

        $rules = array('title' => 'required|min:5');
        $data = Input::get('data');
        $validation = Validator::make($data['Category'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrrors($validation);
        $category = !empty($data['Category']['id']) ? Category::find($data['Category']['id']) : new Category();
        $category->title = $data['Category']['title'];
        $category->slug = Str::slug($data['Category']['title']);
        $category->status = $data['Category']['status'];
        $category->description = $data['Category']['description'];
        $category->exam_video = $data['Category']['exam_video'];

        try {
            $category->save();
            $saveId = $category->id;
            $i = 1;
            foreach ($data['Levels'] as $level) {
                $clevel = !empty($level['id']) ? Category::find($level['id']) : new Category();
                $clevel->title = $level['title'];
                $clevel->slug = Str::slug($level['title']);
                $clevel->parent_id = $saveId;
                $clevel->description = $level['description'];
                $clevel->exam_video = !empty($level['exam_video']) ? $level['exam_video'] : 0;
                $clevel->status = 'on';
                if (empty($level['id']))
                    $clevel->level = $i;
                $clevel->save();
                $i++;
            }
            return Redirect::route('update/categories/admin', $saveId)->with('success', 'Lưu chuyên mục thành công');
        } catch (\Exception $ex) {
            return Redirect::back()->withInput()->withErrors($ex);
        }
        return Redirect::back()->withInput()->withErrors('');

    }

}

?>